﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBService;
using DB2Service;

namespace RXCourt
{
   public class LZZYLW:ICourt
    {
        private  IDBService dbService ;
        private string ConnectionString;
        private string currentFydm="";
        public LZZYLW(IDBService dbService, string ConnectionString)
       {
           this.ConnectionString = ConnectionString;
           this.dbService = dbService;

       }
       static int getTaget(string c)
       {
           int a = 0;
           if (c[0] != '0' && c[1] == '0' && c[2] == '0')
           {
               a = 1;
           }

           if (c[0] != '0' && c[1] != '0' && c[2] == '0')
           {
               a = 2;
           }

           if (!c.Contains("0"))
           {
               a = 3;
           }
           return a;
       }
       static string GetLikeString(string fbdm)
       {
           string c = fbdm;
           string likeStr = "";
           if (c == "000")
           {
               likeStr = "%00";
           }

           switch (getTaget(c))
           {
               case 1:
                   likeStr = string.Format("{0}%0", c[0]);
                   break;
               case 2:
                   likeStr = string.Format("{0}{1}%", c[0], c[1]);
                   break;
               case 3:
                   likeStr = c;
                   break;

           }
           return likeStr;
       }

     #region ICourt 成员
    

     DataTable ICourt.GetChildrenFY(string fydm)
     {
         string fbdm = ((ICourt)this).GetFBDM(fydm);
         string likeStr = GetLikeString(fbdm);
         string sql = string.Format("select dm,fbdm as fjdm ,mc from b_fy where fbdm like '{0}' and fbdm<>'{1}' and (sm is null or sm<>'已撤销')   order by fbdm", likeStr, fbdm);
        return  dbService.Select(sql, ConnectionString);
     }
    
     DataTable ICourt.GetSelfAndChildrenFY(string fydm)
     {

         string fbdm = ((ICourt)this).GetFBDM(fydm);
         string likeStr = GetLikeString(fbdm);
         string sql = string.Format("select dm,fbdm ,mc from b_fy where fbdm like '{0}'  and (sm is null or sm<>'已撤销')   order by fbdm", likeStr);
         return dbService.Select(sql, ConnectionString);
     }

     DataTable ICourt.GetFY(string fydm)
     {
         string sql = string.Format("select b_fy.* from b_fy where dm=", fydm);
         return dbService.Select(sql, ConnectionString);
     }

     string ICourt.GetFY_IDColumnName()
     {
         return "dm";
     }

     string ICourt.GetFY_MCColumnName()
     {
         return "mc";
     }

   

     DataTable ICourt.GetTSs(string fydm)
     {
         string sql = string.Format("SELECT * FROM B_TSJG WHERE SFTJFLAG=1 and fy={0} ORDER BY TSPX ASC", fydm);
         return dbService.Select(sql, ConnectionString);
     }

     DataTable ICourt.GetRYs(string fydm, string tsdm)
     {
         string sql = string.Format("SELECT * FROM B_RYXX where INT(xcbm/65536)={0} and xcbm={1}", fydm, tsdm);
        
         return dbService.Select(sql, ConnectionString);
     }

     


     string ICourt.GetTS_MCColumnName()
     {
         return "mc";
     }

     string ICourt.GetTS_IDColumnName()
     {
         return "xh";
     }

    


     string ICourt.GetRY_IDColumnName()
     {
         return "dm";
     }

     string ICourt.GetRY_MCColumnName()
     {
         return "mc";
     }

  

     string ICourt.GetCurrentFYDM()
     {
         return currentFydm;
     }

   
     void ICourt.SetCurrentFYDM(string fydm)
     {
         currentFydm = fydm;
     }

     string ICourt.GetFBDM(string fydm)
     {
         string sql = string.Format("SELECT FBDM FROM b_fy WHERE DM={0}", fydm);
        object a= dbService.ExcecuteScalar(sql, ConnectionString);
        if (a != null) return a.ToString();
        return "";
     }

     string ICourt.GetFYMC(string fydm)
     {
         ICourt iCourt = this;
         string sql = string.Format("select {0} from {1} where {2}={3}", iCourt.GetFY_MCColumnName(), "b_fy", iCourt.GetFY_IDColumnName(), fydm);
         object o = dbService.ExcecuteScalar(sql, ConnectionString);
         if (o == null) return "";
         return o.ToString();
     }

     string ICourt.GetTSMC(string fydm, string tsdm)
     {
         ICourt iCourt = this;
         string sql = string.Format("select {0} from {1} where FY={4}  and {2}={3}", iCourt.GetTS_MCColumnName(), "B_TSJG", iCourt.GetTS_IDColumnName(), tsdm, fydm);
         object o = dbService.ExcecuteScalar(sql, ConnectionString);
         if (o == null) return "";
         return o.ToString();
     }
     #endregion
    }
}
